package com.example.mylibrarybase.yumengmeng15.utils;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ValueAnimator;

/**
 * Created by 35876 于萌萌
 * 创建日期: 15:06 . 2016年10月24日
 * 描述:
 * <p>
 * <p>
 * 备注:
 */

public class Utilsflexible {

    public static void sumbit(final TextView safe_arrow, final LinearLayout mSafeContent){
        // 默认获取到标记的值
        boolean flag = (boolean) safe_arrow.getTag();
        final ViewGroup.LayoutParams params = mSafeContent.getLayoutParams();
        int height = mSafeContent.getMeasuredHeight();
        int tagetHeight;
        if (flag) {
            safe_arrow.setTag(false);
            tagetHeight = 0;// 别写错了 不要写成height=0
        } else {
            safe_arrow.setTag(true);
            tagetHeight = measureSafeContentHeight(mSafeContent);
        }

        // 为了避免程序的anr异常，所以必须把mSafeContent设置为不可点击状态
        mSafeContent.setEnabled(false);

        // 属性动画,从默认的高度高目标高度的一个变化的过程 0 1000
        ValueAnimator va = ValueAnimator.ofInt(height, tagetHeight);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator arg0) {
                params.height = (int) arg0.getAnimatedValue();
                // 这个地方少写一句话，没有把修改后的参数重新设置给mSafeContent
                mSafeContent.setLayoutParams(params);
            }
        });
        va.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator arg0) {

            }

            @Override
            public void onAnimationRepeat(Animator arg0) {

            }

            @Override
            public void onAnimationEnd(Animator arg0) {
                // 当动画结束的时候，控件就可以点击了
                mSafeContent.setEnabled(true);

                boolean tag = (boolean) safe_arrow.getTag();
                safe_arrow.setText(tag ?"点了":"没点");

//                safe_arrow.setImageResource(tag ? R.drawable.arrow_up
//                        : R.drawable.arrow_down);
            }

            @Override
            public void onAnimationCancel(Animator arg0) {

            }
        });
        // 设置动画的时间
        va.setDuration(500);
        // 开启动画
        va.start();
    }

    /**
     * 测量内容页面的高度
     *
     * @return
     * @param mSafeContent
     */
    private static int measureSafeContentHeight(LinearLayout mSafeContent) {
        int width = mSafeContent.getWidth();
        mSafeContent.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(width,
                View.MeasureSpec.EXACTLY);
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(20000,
                View.MeasureSpec.AT_MOST);

        mSafeContent.measure(widthMeasureSpec, heightMeasureSpec);
        return mSafeContent.getMeasuredHeight();
    }
    }

